#include "list.h"

ListNode* InitListNode()
{
	ListNode* node = (ListNode*)malloc(sizeof(ListNode));
	node->pre = (Status*)malloc(sizeof(Status) * LIST_INIT_SIZE);
	node->size = 0;
	node->capacity = LIST_INIT_SIZE;
	return node;
}

void Push_BackListNode(ListNode* node, Status n)
{
	assert(node);
	if (node->size == node->capacity)
	{
		node->capacity += LISTINCREMENT;
		Status* newnode = (Status*)realloc(node->pre, sizeof(Status) * node->capacity);
		if (newnode == NULL)
		{
			exit(-1);
		}
		node->pre = newnode;
	}
	node->pre[node->size++] = n;
}

void Pop_BackListNode(ListNode* node)
{
	assert(node);
	assert(node->size > 0);
	node->size--;
}

void InsertListNode(ListNode* node, int p, Status n)
{
	assert(node);
	assert(node->size > p && p >= 0);
	if (node->size == node->capacity)
	{
		node->capacity += LISTINCREMENT;
		Status* newnode = (Status*)realloc(node->pre, sizeof(Status) * node->capacity);
		if (newnode == NULL)
		{
			exit(-1);
		}
		node->pre = newnode;
	}
	for (int i = node->size; i > p; i--)
	{
		node->pre[i] = node->pre[i - 1];
	}
	node->pre[p] = n;
	node->size++;
}

void EraseListNode(ListNode* node, int p)
{
	assert(node);
	assert(node->size > p && p >= 0);
	for (int i = p; i < node->size - 1; i++)
	{
		node->pre[i] = node->pre[i + 1];
	}
	node->size--;
}

int FindListNode(ListNode* node, int p)
{
	assert(node);
	for (int i = 0; i < node->size; i++)
	{
		if (node->pre[i] == p)
		{
			return i;
		}
	}
	return -1;
}

int SizeListNode(ListNode* node)
{
	assert(node);
	return node->size;
}

void PrintListNode(ListNode* node)
{
	assert(node);
	for (int i = 0; i < node->size; i++)
	{
		printf("%d ", node->pre[i]);
	}
	printf("\n");
}

void DestoryListNode(ListNode* node)
{
	assert(node);
	free(node->pre);
	node->pre = NULL;
	node->capacity = 0;
	node->size = 0;
}

bool EmptyListNode(ListNode* node)
{
	assert(node);
	return !(node->size);
}

void BlankListNode(ListNode* node)
{
	assert(node);
	node->size = 0;
}
